package com.bjpowernode.p2p.service.user.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.bjpowernode.p2p.dao.ReportDao;
import com.bjpowernode.p2p.service.user.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;
@Slf4j
@Component
@Service(interfaceClass = UserService.class,version = "1.0")
public class UserServiceImpl implements UserService {


    @Autowired
    ReportDao reportDao;

    @Autowired
    RedisTemplate<Object,Object> redisTemplate;


    @Override
    public Integer countUser() {
        Integer countUserReport=(Integer)   redisTemplate.opsForValue().get("countUserReport");
        if(!ObjectUtils.allNotNull(countUserReport)){
            synchronized (this){
                countUserReport=(Integer)   redisTemplate.opsForValue().get("countUserReport");
                if(!ObjectUtils.allNotNull(countUserReport)){
                    countUserReport = reportDao.countUserReport();
                    log.info("数据库查询:countUserReport");
                    redisTemplate.opsForValue().set("countUserReport",countUserReport,30, TimeUnit.SECONDS);
                }
            }
        }
        return countUserReport;
    }
}
